// package1/pages/repair/repair.js
import service from '../../../utils/request'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],
    baseUrl:app.data.url,
    delete: true,
    site: '',
    imageData:[],
    show: false,
    date: '',
    repairTime :'',
    phone: '',
    reasonList: ['不能充电', '机器损坏', '其他原因'],
    reasonIndex: '',
    reason: '',
    gunId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      gunId: options.gunId
    })
  },

  getUser(){
    let that = this
    let data = service('GET','/user/info',{}).then( res => {
      console.log('我获取了我的头像',res)
      if( res.data.code ==0){
        that.setData({
          phone: res.data.user.phone
        })
      }
    })
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    console.log('onConfirm', event)
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
  },
  bindPickerChange(e){
    console.log(e)
    this.setData({
      reasonIndex: e.detail.value
    })
  },
  chooseTime(option){
    console.log('chooseTime', option)
    this.setData({
      repairTime : option.detail
    })
  },
  chooseSite(option){
    console.log('chooseSite', option)
    this.setData({
      site: option.detail
    })
  },
  afterRead(event) {
    console.log('event', event)
    const { file } = event.detail;
    let that = this
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      header:{
        token: wx.getStorageSync('token'),
        'accept': 'application/json',
      },
      url: this.data.baseUrl+'/file/pile', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: { user: 'test' },
      success(res) {
        console.log('res',res)
        let data = JSON.parse(res.data)
        console.log(data)
        // 上传完成需要更新 fileList
        const { fileList = [] } = that.data;
        fileList.push({ ...file, url:that.data.baseUrl+data.file, deletable: true, base: data.file});
        that.setData({ fileList });
        console.log('uploadFile', that.data.fileList)
      },
    })
    
  },
  deletePic(e){
    console.log('删除图片' , e)
    let data = this.data.fileList
    data.splice(e.detail.index, 1)
    console.log(data)
    this.setData({
      fileList: data
    })
  },

  updateData(){
    if(!this.data.reasonIndex){
      wx.showToast({
        title: '请选择维修原因',
        icon: 'error'
      })
      return
    }
    let gunId =this.data.gunId,  reason = this.data.reasonList[this.data.reasonIndex]
    console.log('gunId',gunId)
    let file = this.data.fileList
    let pics = [], phone = this.data.phone
    if(file.length > 0){
      file.map(item => {
        pics.push(item.base)
      })
    }
    console.log('提交图片',pics )
    service('POST', '/personal/repair/repair', {
      pics, gunId , phone, reason
    }).then((res) => {
      if(res.data.code ==0){
        wx.showToast({
          title: res.data.msg,
          icon: 'success'
        })
        wx.navigateTo({
          url: '/package1/pages/repair/index',
        })
      }else{
        wx.showToast({
          title: '提交失败',
          icon: 'error'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})